<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <title>SSE 实时消息推送示例</title>
</head>
<body>
    <div>
        接收消息
    </div>
    <script type="text/javascript">
        // 创建 EventSource 对象连接后端
        var source = new EventSource('http://localhost:8080/stream');

        // 处理接收到的消息
        source.onmessage = function (event) {
          console.log(event.data);
          var div = document.createElement('div');
          div.innerHTML = event.data;
          document.body.appendChild(div);
        };

        // 处理连接错误
        source.onerror = function (error) {
          console.error('SSE 连接错误:', error);
        };
    </script>
</body>
</html>